<template>
  <div>
    <!-- 
      el-table
      树形数据
      支持树类型的数据的显示。当 row 中包含 children 字段时，被视为树形数据。渲染树形数据时，必须要指定 row-key。
      row-key="id"
      tree-props	渲染嵌套数据的配置选项	Object	—	{ hasChildren: 'hasChildren', children: 'children' }
      default-expand-all	是否默认展开所有行，当 Table 包含展开行存在或者为树形表格时有效	Boolean
     -->
    <el-table
      :data="menulist"
      border
      style="width: 100%"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      default-expand-all
    >
      <el-table-column prop="id" label="菜单编号" width="180">
      </el-table-column>
      <el-table-column prop="title" label="菜单名称" width="180">
      </el-table-column>
      <el-table-column prop="pid" label="上级菜单"> </el-table-column>
      <el-table-column label="菜单图标" width="180">
        <!-- 开辟一个作用域槽口 用于修改数据内容
        item 随便自定义
        row 是el-table 固定的 代表行
        item.row 每一行的数据
        -->
        <template slot-scope="item">
          <div>
            <!-- {{item.row.icon}} -->
            <i :class="item.row.icon"></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="菜单地址" width="180">
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="item">
          <div>
            {{ item.row.status == 1 ? "开启" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
              @click="edit(item.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              circle
              @click="del(item.row.id)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
//引入辅助性函数
import { mapGetters, mapActions } from "vuex";
//引入封装好的接口
import { menuDel } from "../../request/api";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenu",
    }),
  },
  mounted() {
    //页面一加载调用接口地址
    this.getMenuAction();
    //console.log(this.$store,'仓库')
  },
  methods: {
    ...mapActions({
      getMenuAction: "menu/getMenuAction",
    }),
    //封装一个编辑事件，打开弹框
    edit(id) {
      this.$emit("edit",id);
    },
    //封装一个删除事件
    del(id) {
      this.$confirm("你确定要删除这条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除接口
          menuDel({ id }).then((res) => {
            // console.log(res, "删除");
            if (res.code == 200) {
              this.$message.success(res.msg);
              //重新触发行动
              this.getMenuAction();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped></style>
